﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Microsoft.Practices.Unity;
using Wicresoft.UnifyShow.Business;

namespace Wicresoft.UnifyShow2.Portal.Controllers
{
    [Authorize]
    public class AdminEventMaintainController : BaseController
    {
        private IUser_X user;
        private ISiteBranch siteBranch;
        private ISelfEvent selfevent;
        private IItsmEventType itsmEvent;

        protected override void Dispose(bool disposing)
        {
            ((IDisposable)user).Dispose();
            ((IDisposable)siteBranch).Dispose();
            ((IDisposable)selfevent).Dispose();
            ((IDisposable)itsmEvent).Dispose();
        }

        [InjectionConstructor]
        public AdminEventMaintainController(IUser_X user, ISiteBranch siteBranch, ISelfEvent selfevent, IItsmEventType itsmEvent)
        {
            this.user = user;
            this.siteBranch = siteBranch;
            this.selfevent = selfevent;
            this.itsmEvent = itsmEvent;
        }

        //
        // GET: /EventMaintain/

        public ActionResult Index()
        {
            base.SetRoles(user);
            ViewBag.IsConfigAdmin = IsConfigAdmin;
            var USER_X = user.GetUser_XByName(User.Identity.Name);
            if (USER_X == null)
                throw new Exception("USER_X is null.");
            ViewBag.UserDept = USER_X.Department;
            ViewBag.UserCompany = USER_X.Company;

            if (!IsConfigAdmin) return new HttpStatusCodeResult(403);


            var listModel = itsmEvent.GetAllEvent();
            return View(listModel);
        }

        public ActionResult Tree()
        {
            base.SetRoles(user);
            ViewBag.IsConfigAdmin = IsConfigAdmin;
            var USER_X = user.GetUser_XByName(User.Identity.Name);
            if (USER_X == null)
                throw new Exception("USER_X is null.");
            ViewBag.UserDept = USER_X.Department;
            ViewBag.UserCompany = USER_X.Company;

            if (!IsConfigAdmin) return new HttpStatusCodeResult(403);
            var listModel = itsmEvent.GetAllEvent();

            ViewData["itsnTier1"] = listModel.Select(o => o.TIER1).Distinct();
            IEnumerable<string> t2list = listModel.Select(o => o.TIER2).Distinct();
            List<EventTypeModel> evenlist = new List<EventTypeModel>();
            foreach (var item in t2list)
            {
                EventTypeModel m = listModel.Where(o => o.TIER2 == item).FirstOrDefault();
                evenlist.Add(m);
            }

            ViewData["itsnTier2"] = evenlist;


            return PartialView(listModel);
        }

    }
}
